package com.e;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet1
 */
public class LoginServlet1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
	    String nameString = request.getParameter("userName");
	    String pwdString = request.getParameter("pwd");
	    //
	    System.out.println(nameString+"---");
	    System.out.println(pwdString+"---");
	    HttpSession session=request.getSession();
	    
	    if(!nameString.equals("")&&!pwdString.equals("")){
	        if(nameString.equals("admin")) {
	          // 代表用户名正确
	            if(pwdString.equals("123456")) {
	              // 密码正确
	            	session.setAttribute("userName", nameString);
	            	session.setAttribute("pwd", pwdString);
	            	response.sendRedirect("UserServlet");
	            }else {
	              // 密码错误
	            	System.out.println("密码错误");
	            	session.setAttribute("msg", "密码错误");
	            	response.sendRedirect("login.jsp");
	            }
	        }else {
	          // 用户名错误重新输   
	        	System.out.println("用户名错误重新输");
            	session.setAttribute("msg", "该用户不存在，是否注册呢?");
            	response.sendRedirect("login.jsp");
	        }
	    }else {
	      // 你的用户名或者密码有一个是空的
	      System.out.println("-----");
	     
	      session.setAttribute("msg", "用户名或者密码有一个是空");
	      request.getRequestDispatcher("login.jsp").forward(request, response);
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
